package com.jdbc;

import java.sql.*;

public class Demo {
    static final String DB_URL = "jdbc:mysql://106.52.210.59:3306/gradembak";
    static final String USER = "root";
    static final String PSW = "78.432bCF0123qqc";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet=null;
        int i=0;
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获取 Connection对象
            connection = DriverManager.getConnection(DB_URL, USER, PSW);
            //3.定义sql语句
            String sql = "select * from student";
            //4.获取Statement对象
            statement = connection.createStatement();
            //5.执行sql语句
//            int i = statement.executeUpdate(sql);
//            System.out.println(i);
//            if (i>0){
//                System.out.println("修改成功");
//            }else {
//                System.out.println("修改失败");
//            }
            resultSet = statement.executeQuery(sql);
            System.out.println(resultSet);
            while (resultSet.next()){
                String sno= resultSet.getString("sno");
                String sname=resultSet.getString("sname");
                String ssex= resultSet.getString("ssex");
                String sphone= resultSet.getString("sphone");
                String saddress= resultSet.getString("saddress");
                String sbirthday= resultSet.getString("sbirthday");
                String classno= resultSet.getString("classno");
                String qq= resultSet.getString("QQ");
                System.out.println(sno+"\t"+sname+"\t"+ssex+"\t"+sphone+"\t"+saddress+"\t"+sbirthday+"\t"+classno+"\t"+qq);
                i++;

            }
            System.out.println(i);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }
}
